<?php
//print_r($argv);
$filename=$argv[1];
//echo "the config file is :".$filename;
//echo "\n";


if (file_exists($filename)) {
        //echo "The file $filename exists\n";
        $handle = fopen($filename, "r");
        while (!feof($handle)){
                $line = fgets($handle);
                if((trim($line)!=NULL)&&(substr($line,0,1)!="#")){
                        //echo "sssssssssssssssssite: ",$line,"\n";
                        $srvpr = explode(",", $line);
                        //print_r($srvpr);
                        ftp_download($srvpr[0],$srvpr[1],$srvpr[2],$srvpr[3],NULL);
                }
        }
                fclose($handle);
} else {
    echo "The config file $filename does not exist\n";
}



function  ftp_download (
$ftp_server,
$ftp_user_name,
$ftp_user_pass,
$source_file,
$destination_file
){
$ftp_server=trim($ftp_server);
$ftp_user_name=trim($ftp_user_name);
$ftp_user_pass=trim($ftp_user_pass);
$source_file=trim($source_file);

if (is_NULL($destination_file)){
        $destination_file=$ftp_server.'_'.$source_file;
        //echo $destination_file;
        }

// set up basic connection
$conn_id = ftp_connect($ftp_server);
if($conn_id){
        // login with username and password
        $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
        //echo $login_result;
        //echo "$ftp_server"."$source_file"."test\n";
        if($login_result){
                $download=ftp_get($conn_id, $destination_file, $source_file, FTP_BINARY);
                //check download status
                if (!$download) {
                echo "FTP Download from $ftp_server has failed!\n";
                } else {
                echo "Downloaded $source_file to $destination_file from $ftp_server\n";
                }
                // close the FTP stream

                } else{
                echo "Can't login to $ftp_server, for user $ftp_user_name.\n";
                }
        ftp_close($conn_id);
        }else{
                echo "Can't connect to $ftp_server, for user $ftp_user_name.\n";
        }
}
?>
